package com.joker.aigc.accountboolk.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.joker.aigc.accountboolk.domain.dto.AccountBookDTO;
import com.joker.aigc.accountboolk.domain.dto.AccountBookQueryDTO;
import com.joker.aigc.accountboolk.domain.vo.AccountBookVO;
import com.joker.aigc.accountboolk.service.AccountBookService;
import com.joker.aigc.accountboolk.service.AiService;
import com.joker.aigc.domain.entity.R;
import com.joker.aigc.utils.AssertUtil;
import com.joker.aigc.utils.RequestHolder;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @description:
 * @author: feixiang.li
 * @date: 2025-01-04 13:26
 */
@RestController
@RequestMapping("/api/1.0/finance")
@Validated
@Tag(name = "finance", description = "记账本")
@Slf4j
public class FinanceController {

    @Resource
    private AccountBookService accountBookService;

    @Resource
    AiService aiService;

    @PostMapping("/account")
    public R<List<AccountBookVO>> list(@RequestBody AccountBookQueryDTO accountBookQueryDTO) {
        AssertUtil.isTrue(accountBookQueryDTO.getPageNum() > 0, "页码不能小于1");
        AssertUtil.isTrue(accountBookQueryDTO.getPageSize() > 0, "每页条数不能小于1");
        Long uid = RequestHolder.get().getUid();
        if (Objects.isNull(uid)) {
            accountBookQueryDTO.setUserId(0L);
        }
        // 如果结束时间不为空，将结束时间设置为 结束时间时间当天的23:59:59
        if (Objects.nonNull(accountBookQueryDTO.getEndTime())) {
            accountBookQueryDTO.setEndTime(DateUtil.endOfDay(accountBookQueryDTO.getEndTime()));
        }
        accountBookQueryDTO.setUserId(uid);
        log.info("记账本查询参数:{}", accountBookQueryDTO);
        return R.ok(accountBookService.list(accountBookQueryDTO));
    }

    @PostMapping("/account/add")
    public R<String> add(@RequestBody AccountBookDTO accountBookVO) {
        accountBookVO.setId(null);
        accountBookVO.setUserId(RequestHolder.get().getUid());
        log.info("记账本添加参数:{}", accountBookVO);
        accountBookService.save(accountBookVO);
        return R.ok("添加成功");
    }

    @PostMapping("/account/update")
    public R<String> update(@RequestBody AccountBookDTO accountBookVO) {
        accountBookService.updateById(accountBookVO);
        return R.ok("修改成功");
    }


    @PostMapping("/account/delete")
    public R<String> delete(@RequestBody AccountBookQueryDTO accountBookQueryDTO) {
        AssertUtil.isTrue(CollUtil.isNotEmpty(accountBookQueryDTO.getIds()), "删除失败，请选择要删除的数据");
        accountBookService.deleteBatchById(accountBookQueryDTO.getIds());
        return R.ok("删除成功");
    }

    /**
     * 这里用来接收一段音频数据，然后自动解析成账单
     *
     * @param file mp3格式的音频文件
     * @return 账单列表
     */
    @PostMapping(value = "/account/automatic",headers = {
            "content-type=multipart/form-data"
    })
    public R<List<AccountBookVO>> automatic(@RequestParam("file") MultipartFile file) {
        log.info("自动解析账单参数:{}", file);
        return R.ok(aiService.convert(file));
    }

}
